package org.springframework.fusion.webscoket.controller;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.fusion.webscoket.config.WebSocketConnection;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

@RestController
public class UserController {

	@Autowired
	private WebSocketConnection webSocketMessage;

	@GetMapping("test")
	public ResponseEntity<String> test(){
		ConcurrentHashMap<String, WebSocketSession> sessionList = webSocketMessage.getSessionList();
		for (Map.Entry<String, WebSocketSession> entry : sessionList.entrySet()) {
			WebSocketSession session = entry.getValue();
			try {
				session.sendMessage(new TextMessage(String.valueOf(new Date().getTime())));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ResponseEntity.ok("OK!!!!!!!!!!!!");
	}
}
